import {defineStore} from 'pinia'

// 动态tab
export const useTabStore = defineStore('tab',{
  state:()=>({
    tabList:[{
      path:'/dashboard',
      name:'index',
      meta:{
        title:'首页',
        affix:true
      }
    }]
  }),
  persist:true,
  actions:{
    async addTab(tab){
    if (this.tabList.some(item => item.path === tab.path)) return;
      this.tabList.push(tab);
    },
    async delOtherTab(route){
      this.tabList = this.tabList.filter(
        (item) => item.meta.affix || item.path === route.path
      )
    },
    async delLeftTab(route){
      let index = this.tabList.length
      this.tabList = this.tabList.filter((item) => {
      if (item.path === route.path) index = this.tabList.indexOf(item)
      console.log(item.meta.affix || index <= this.tabList.indexOf(item));
      return item.meta.affix || index <= this.tabList.indexOf(item)
    })
    },
    async delRightTab(route){
      let index = this.tabList.length
      this.tabList = this.tabList.filter((item) => {
      if (item.path === route.path) index = this.tabList.indexOf(item)
      console.log(item.meta.affix || index <= this.tabList.indexOf(item));
      return item.meta.affix || index >= this.tabList.indexOf(item)
    })
    },
    async delAllTab(route){
      this.tabList =  this.tabList.filter((item) => item.meta.affix)
    }
  },
  getters:{}
})